package org.school.controller;

import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.school.entity.Cai;
import org.school.entity.Dingdan;
import org.school.entity.Shop;
import org.school.entity.message2;
import org.school.service.ShopService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Shop)表控制层
 *
 * @author makejava
 * @since 2023-01-13 17:44:44
 */
@RestController
@RequestMapping("shop")
@CrossOrigin
@Log4j2
public class ShopController {
    /**
     * 服务对象
     */
    @Resource
    private ShopService shopService;

    /**
     * 分页查询
     *
     * @param shop 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Shop>> queryByPage(Shop shop, PageRequest pageRequest) {
        return ResponseEntity.ok(this.shopService.queryByPage(shop, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Shop> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.shopService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param shop 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Shop> add(Shop shop) {
        return ResponseEntity.ok(this.shopService.insert(shop));
    }

    /**
     * 编辑数据
     *
     * @param shop 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Shop> edit(Shop shop) {
        return ResponseEntity.ok(this.shopService.update(shop));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */


    @GetMapping("allCai")//使用分页查询查询菜品
    public ResponseEntity<PageInfo<Cai>> getCaiByPage(String shopname, int page, int size ) {

        return ResponseEntity.ok((PageInfo<Cai>) this.shopService.findCaiInfo(shopname, page, size));
    }
    @DeleteMapping("cai")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.shopService.deleteById(id));
    }
    @PutMapping("col")//用户收到订单
    public ResponseEntity comByUser(@Param("shop") String shop, @Param("id") int id) {
        message2 mes=this.shopService.completeOrder(shop,id);
        return ResponseEntity.ok().body( mes);
    }
    @GetMapping("order")//用户查看自己的订单
    public ResponseEntity<List<Dingdan>> getOrder(@Param("name") String name) {
        return ResponseEntity.ok().body( this.shopService.querryOrderByShop(name));
    }
}

